/**
 * Created by 89004804 on 2018/1/14.
 */
utils = {};
utils.message = {};
utils.message.confirm = function(option) {
    $('.confirm-dialog-title').text((option && option.title) || '确认');
    $('.confirm-dialog-body').html((option && option.content) || '');

    if(option && option.contentBind) {
        option.contentBind();
    }

    $('.confirm-dialog-no').text((option && option.noText) || '取消');
    $('.confirm-dialog-ok').html((option && option.yesText) || '确认');

    if(option && option.yesBind) {
        $('.confirm-dialog-ok').unbind("click");
        $('.confirm-dialog-ok').click(function() { if(option.yesBind((option.data || {}))){ $('#confirm-dialog').modal('hide');}});
    } else if(option && option.cancelBind) {
        $('.confirm-dialog-no').unbind("click");
        $('.confirm-dialog-no').click(function() { (option.cancelBind || function(){})();});
    } else {
        console.log('没有给确认按钮绑事件，弹框无效！！');
    }

    $('#confirm-dialog').modal({'keyboard':false}).on('hidden.bs.modal', function (e) {
        (option.cancelBind || function(){} )();
    })
};

var messageList = {};
utils.showMessage = function(msg, type, time) {
    if (!msg || msg.trim().length === 0) return;
    var cnt = 0;
    if (messageList[msg]) {
        cnt = messageList[msg].cnt + 1;
        messageList[msg].cnt = cnt;
        messageList[msg].noty.close();
    }

    var cls = 'alert-danger';
    if (type) {
        switch (type) {
            case 'error':
                break;
            case 'success':
                cls = 'alert-success';
                break;
            case 'info':
                cls = 'alert-info';
                break;
            case 'warning':
                cls = 'alert-warning';
                break;
        }
    }

    //排序多余5个了删除
    var msglist = [];
    for (var key in messageList) {
        msglist.push(messageList[key]);
    }

    //排序
    function compact(v1, v2) {
        if (v1.lastTime < v2.lastTime) {
            return 1;
        } else if (v1.lastTime > v2.lastTime) {
            return -1;
        } else {
            return 0;
        }
    }
    msglist.sort(compact);
    var delCnt = msglist.length - 4;
    for (var i = 0; i < delCnt; i++) {
        messageList[msglist.pop().msg].noty.close();
    }

    var text = '<span>' + msg + '</span>';
    if (cnt !== 0) {
        text = '<div class="noty_text_container"><p>' + msg + '</p><i>' + (cnt + 1) + '</i></div>';
    }

    var n = noty({
        text: text,
        type: type,
        dismissQueue: true,
        layout: 'topCenter',
        theme: 'base-noty',
        maxVisible:1,
        killer:true,
        callback: {
            onClose: function() {
                delete messageList[msg];
            }
        }
    });

    messageList[msg] = {
        noty: n,
        cnt: cnt,
        msg: msg,
        lastTime: (new Date()).valueOf()
    };

    if ('error' !== type) {
        setTimeout(function() {
            n.close();
        }, time || 3000);
    }
    return n;
};